import * as wasm from '../pkg/wasm.js';

const btn = document.querySelector('#btn');
const img = document.querySelector('#img');
// let utf8decoder = new TextDecoder();
// let encode = wasm.encode_aes('dks1234中文,，；.。');
// let decode = wasm.decode_aes(encode);

// console.log(`encode is : ${encode}`);
// console.log(`decode is : ${decode}`);
// console.log(format(decode));

// wasm.set_item('test', 'val');
btn.addEventListener('click', async function(e) {
  const heic = await fetch('1.heic');
  const arrayBuffer = await heic.arrayBuffer();
  let buffer = wasm.heic_to_jpg(new Uint8Array(arrayBuffer));
  const blob = new Blob([buffer], { type: 'image/jpeg'});
  buffer = null;
  const url = URL.createObjectURL(blob);
  img.src = url;
}, false);

function format(data) {
  let ret = ''
  for (let i=0; i<data.length; i++) {
    ret += String.fromCharCode(data[i]);
  }
  return ret;
}
